﻿#include "ChangeGuninfo.h"
#include "ui_changeguninfo.h"

ChangeGuninfo::ChangeGuninfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChangeGuninfo)
{
    ui->setupUi(this);
    ui->labelSystemHint->hide();
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    MeterModel = new QSqlQueryModel(this);
    MeterMapper = new QDataWidgetMapper(this);
    MeterMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    MeterMapper->setModel(MeterModel);

    keyBoardConnect();
}

ChangeGuninfo::~ChangeGuninfo()
{
    delete ui;
}
void ChangeGuninfo::TitleChange(int gunnum)
{
    ui->labelTitle->setText(QString("%1号枪信息修改").arg(gunnum+1));
    ChangeGunNo = gunnum;
    //读取数据
    readGuninfodb();
    showguninfo(gunnum);
}
void ChangeGuninfo::showguninfo(int gunnum)
{
    ui->m_editMt_port->setText(MeterModel->data(MeterModel->index(gunnum, 4)).toString());
    Save[0] = ui->m_editMt_port->text().trimmed();

    ui->m_editPhase_num->setText(MeterModel->data(MeterModel->index(gunnum, 5)).toString());
    Save[1] = ui->m_editPhase_num->text().trimmed();

    ui->m_editMeteraddr->setText(MeterModel->data(MeterModel->index(gunnum, 6)).toString());
    Save[2] = ui->m_editMeteraddr->text().trimmed();

    ui->m_editCT->setText(MeterModel->data(MeterModel->index(gunnum, 7)).toString());
    Save[3] = ui->m_editCT->text().trimmed();

    ui->m_editGunno->setText(MeterModel->data(MeterModel->index(gunnum, 0)).toString());
    Save[4] = ui->m_editGunno->text().trimmed();

    ui->m_editCan_port->setText(MeterModel->data(MeterModel->index(gunnum, 1)).toString());
    Save[5] = ui->m_editCan_port->text().trimmed();

    ui->m_editFrame_gunno->setText(MeterModel->data(MeterModel->index(gunnum, 2)).toString());
    Save[6] = ui->m_editFrame_gunno->text().trimmed();

    ui->m_editCha_addr->setText(MeterModel->data(MeterModel->index(gunnum, 3)).toString());
    Save[7] = ui->m_editCha_addr->text().trimmed();
}
void ChangeGuninfo::readGuninfodb()
{
    QString dataBasePath = "/usr/local/sanway/dbTest/tcuguninfo.db";
    QFile file(dataBasePath);
    if( file.exists() )
    {
        QSqlDatabase meterDb ;
        if( QSqlDatabase::contains("qt_sql_default_connection") )
        {
            meterDb = QSqlDatabase::database("qt_sql_default_connection");
        }
        else
        {
            meterDb = QSqlDatabase::addDatabase("QSQLITE");
        }

        meterDb.setDatabaseName(dataBasePath);
        if( !meterDb.open() )
        {
            return;
        }
        MeterModel->setQuery("select * from tagGunInfo");
        meterDb.close();
    }
    else
    {
        printf("枪信息数据库不存在!\n");
    }
}
void ChangeGuninfo::on_buttonBack_clicked()
{
    stackLayout->setCurrentIndex(GuninfoSettingPage);
}

void ChangeGuninfo::on_buttonSave_clicked()
{
    //保存按钮
    QString _1 = ui->m_editMt_port->text().trimmed();
    if( _1 != Save[0])
    {
        QString input = QString("/usr/local/sanway/set_guninfo -guninfo -u gunno=%1 mt_port=%2").arg(ChangeGunNo).arg(_1);
        system(input.toLocal8Bit().constData());
    }

    QString _2 = ui->m_editPhase_num->text().trimmed();
    if( _2 != Save[1])
    {
        QString input = QString("/usr/local/sanway/set_guninfo -guninfo -u gunno=%1 phase_num=%2").arg(ChangeGunNo).arg(_2);
        system(input.toLocal8Bit().constData());
    }

    QString _3 = ui->m_editMeteraddr->text().trimmed();
    if( _3 != Save[2])
    {
        QString input = QString("/usr/local/sanway/set_guninfo -guninfo -u gunno=%1 meteraddr=%2").arg(ChangeGunNo).arg(_3);
        system(input.toLocal8Bit().constData());
    }

    QString _4 = ui->m_editCT->text().trimmed();
    if( _4 != Save[3])
    {
        QString input = QString("/usr/local/sanway/set_guninfo -guninfo -u gunno=%1 ct=%2").arg(ChangeGunNo).arg(_4);
        system(input.toLocal8Bit().constData());
    }

    QString _5 = ui->m_editGunno->text().trimmed();
    if( _5 != Save[4])
    {
        QString input = QString("/usr/local/sanway/set_guninfo -guninfo -u gunno=%1 gunno=%2").arg(ChangeGunNo).arg(_5);
        system(input.toLocal8Bit().constData());
    }

    QString _6 = ui->m_editCan_port->text().trimmed();
    if( _6 != Save[5])
    {
        QString input = QString("/usr/local/sanway/set_guninfo -guninfo -u gunno=%1 can_port=%2").arg(ChangeGunNo).arg(_6);
        system(input.toLocal8Bit().constData());
    }

    QString _7 = ui->m_editFrame_gunno->text().trimmed();
    if( _7 != Save[6])
    {
        QString input = QString("/usr/local/sanway/set_guninfo -guninfo -u gunno=%1 frame_gunno=%2").arg(ChangeGunNo).arg(_7);
        system(input.toLocal8Bit().constData());
    }

    QString _8 = ui->m_editCha_addr->text().trimmed();
    if( _8 != Save[7])
    {
        QString input = QString("/usr/local/sanway/set_guninfo -guninfo -u gunno=%1 cha_addr=%2").arg(ChangeGunNo).arg(_8);
        system(input.toLocal8Bit().constData());
    }


    ui->labelSystemHint->show();
    QTimer::singleShot(3000,ui->labelSystemHint,SLOT(hide()));//显示三秒后隐藏

}

void ChangeGuninfo::keyHandle(QPushButton* btn, QLineEdit* lineEdit)
{
    if( btn->text() == tr("退格") )
    {
        lineEdit->backspace();
    }
    else if( btn->text() == tr("清除"))
    {
        lineEdit->clear();
    }
    else
    {
        lineEdit->insert(btn->text());
    }
}

void ChangeGuninfo::on_Keyboard_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());

    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();

    keyHandle(btn, focus);
}

void ChangeGuninfo::keyBoardConnect()
{
    connect(ui->One, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Tow, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Three, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Four, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Five, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Six, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Seven, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Eight, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Nine, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Zero, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Remove, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->RemoveAll, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char1, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char2, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char3, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
}
